package operator;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;

/**
 * Global Window 根据数据的条数划分窗口
 *
 * non-key 的DataStream  window 和 window function的并行度一致
 *
 */
public class CountWindowAllDemo {

    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());

        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        SingleOutputStreamOperator<Integer> nums = lines.map(Integer::parseInt);

        // 根据数据条数 划分窗口
        AllWindowedStream<Integer, GlobalWindow> window = nums.countWindowAll(5);

        SingleOutputStreamOperator<Integer> sumed = window.sum(0);

        sumed.print();

        env.execute();
    }
}
